module Api
  class TodosController < BaseController
    before_action :set_todo, only: [:destroy, :edit, :update, :toggle]
    def index
      @todos = current_store.todos.unfinished.order("id desc")
      render json: {todos: @todos}
    end

    def create
      todo = current_store.todos.create!(content: params[:content])
      render json: {status: true, todo: todo}
    end

    def destroy
      @todo.destroy
      render json: {status: true}
    end

    def edit

    end

    def update
      @todo.update(content: params[:content])
      render json: {status: true, todo: @todo}
    end

    def finished
      @todos = current_store.todos.finished.order("id desc".freeze)
      render json: {todos: @todos}
    end

    def list
      @todos = current_store.todos.order("id desc".freeze)
      render json: {todos: @todos}
    end

    def toggle
      @todo.toggle!
      render json: {status: true}
    end

    def delete
      current_store.todos.to_delete(params[:ids])
      render json: {status: true}
    end

    private
    def set_todo
      @todo = StoreTodo.find(params[:id])
    end
  end
end
